package com.pr.levin.dataideal;

import android.util.Log;

import org.apache.commons.dbcp.BasicDataSource;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 获取连接参数，做好基本的连接数据库
 * Created by levin on 2018/10/30.
 */

public class LinkTodatabase {
    private static BasicDataSource dataSource = null;
    public static Properties properties = new Properties();

    public LinkTodatabase() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * 加载db.properties配置文件，连接数据库参数
     */
    public static void init() {
        Properties dbProps = new Properties();
        ClassLoader cl = LinkTodatabase.class.getClassLoader();
        try {
//            dbProps.load(cl.getResourceAsStream("/com/pr/levin/db.properties"));
            dbProps.load(LinkTodatabase.class.getResourceAsStream("/assets/db.properties"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e("【数据连接情况】", "获取配置文件出错！");
            e.printStackTrace();
        }

        try {
            String driverClassName = dbProps.getProperty("jdbc.driverClassName");
            String url = dbProps.getProperty("jdbc.url");
            String username = dbProps.getProperty("jdbc.username");
            String password = dbProps.getProperty("jdbc.password");

            String initialSize = dbProps.getProperty("dataSource.initialSize");
            String maxIdle = dbProps.getProperty("dataSource.maxIdle");
            String minIdle = dbProps.getProperty("dataSource.minIdle");
            String maxActive = dbProps.getProperty("dataSource.maxActive");
            String maxWait = dbProps.getProperty("dataSource.maxWait");
            Log.d("加载连接参数......", "全部加载完成");

            dataSource = new BasicDataSource();
            dataSource.setDriverClassName(driverClassName);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);

            //初始化连接数
            if (initialSize != null) {
                dataSource.setInitialSize(Integer.parseInt(initialSize));
            }
            //最小连接空闲
            if (minIdle != null) {
                dataSource.setMinIdle(Integer.parseInt(minIdle));
            }
            //最大连接空闲
            if (maxIdle != null) {
                dataSource.setMaxIdle(Integer.parseInt(maxIdle));
            }
            //超时回收时间
            if (maxWait != null) {
                dataSource.setMaxWait(Long.parseLong(maxWait));
            }
            //最大连接数
            if (maxActive != null) {
                if (!maxActive.trim().equals("0")) {
                    dataSource.setMaxActive(Integer.parseInt(maxActive));
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            Log.e("【数据连接情况】", "创建连接池失败！请检查配置文件");
        }
    }

    public static synchronized Connection getConnection() throws SQLException {
        if (dataSource == null) {
            init();
            Log.d("初始化情况", "初始化成功通过！");
        }

        Connection conn = null;
        if (dataSource != null) {
            conn = dataSource.getConnection();
        }
        return conn;
    }

    public static void begin() {
        try {
            Connection conn = getConnection();
            if (conn != null) {
                conn.setAutoCommit(false);
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("【数据连接情况】", "开始事务失败！");
            e.printStackTrace();
        }
    }

    public static void commit() {
        try {
            Connection conn = getConnection();
            if (conn != null) {
                conn.commit();
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("【数据连接情况】", "提交事务失败！");
            e.printStackTrace();
        }
    }

    public static void rollback() {
        try {
            Connection conn = getConnection();
            if (conn != null) {
                conn.rollback();
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("【数据连接情况】", "回滚事务失败！");
            e.printStackTrace();
        }
    }
}
